package cn.jtcoding.order.web.controller;

import cn.jtcoding.common.framework.enums.OrderStatusEnum;
import cn.jtcoding.common.framework.enums.PayMethod;
import cn.jtcoding.order.api.OrderService;
import cn.jtcoding.order.pojo.bo.SubmitOrderBO;
import cn.jtcoding.order.pojo.dataobject.OrderStatusDO;
import cn.jtcoding.order.pojo.vo.OrderVO;
import cn.jtcoding.common.framework.enums.ApiResultCode;
import cn.jtcoding.common.framework.vo.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/create")
    public ApiResult<?> create(@RequestBody SubmitOrderBO submitOrderBO) {
        if (!submitOrderBO.getPayMethod().equals(PayMethod.WECHAT.type)
                && !submitOrderBO.getPayMethod().equals(PayMethod.ALIPAY.type)) {
            return ApiResult.failure(ApiResultCode.PARAM_IS_INVALID);
        }

        // 1. 创建订单
        OrderVO orderVO = orderService.createOrder(submitOrderBO);
        String orderId = orderVO.getOrderId();

        // 通过MQ处理支付订单
        // 2. 创建订单以后，移除购物车中已结算（已提交）的商品
        /**
         * 1001
         * 2002 -> 用户购买
         * 3003 -> 用户购买
         * 4004
         */
        // TODO 整合redis之后，完善购物车中的已结算商品清除，并且同步到前端的cookie
//        CookieUtils.setCookie(request, response, FOODIE_SHOPCART, "", true);

        // 3. 向支付中心发送当前订单，用于保存支付中心的订单数据
        /*
        MerchantOrdersVO merchantOrdersVO = orderVO.getMerchantOrdersVO();
        merchantOrdersVO.setReturnUrl(payReturnUrl);

        // 为了方便测试购买，所以所有的支付金额都统一改为1分钱
        merchantOrdersVO.setAmount(1);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("imoocUserId", "imooc");
        headers.add("password", "imooc");

        HttpEntity<MerchantOrdersVO> entity =
                new HttpEntity<>(merchantOrdersVO, headers);

        ResponseEntity<IMOOCJSONResult> responseEntity =
                restTemplate.postForEntity(paymentUrl,
                        entity,
                        IMOOCJSONResult.class);
        IMOOCJSONResult paymentResult = responseEntity.getBody();
        if (paymentResult.getStatus() != 200) {
            logger.error("发送错误：{}", paymentResult.getMsg());
            return IMOOCJSONResult.errorMsg("支付中心订单创建失败，请联系管理员！");
        }
        */
        return ApiResult.success(orderId);
    }

    @PostMapping("notifyMerchantOrderPaid")
    public ApiResult<?> notifyMerchantOrderPaid(String merchantOrderId) {
        orderService.updateOrderStatus(merchantOrderId, OrderStatusEnum.WAIT_DELIVER.type);
        return ApiResult.success();
    }

    @PostMapping("getPaidOrderInfo")
    public ApiResult<OrderStatusDO> getPaidOrderInfo(String orderId) {
        OrderStatusDO orderStatus = orderService.queryOrderStatusInfo(orderId);
        return ApiResult.success(orderStatus);
    }

}
